package com.wzy.scns.repositery;

import com.wzy.scns.entity.Course;
import com.wzy.scns.entity.SelectCourse;
import com.wzy.scns.vo.SSCourseAndTMsgInterfaceVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Repository
public interface CourseRepository extends JpaRepository<Course, Integer> {

    @Query("select c.code as code,c.name as name,c.credit as credit,t.name as teacherName from Course c,Teacher t where c.teacherId = t.id")
    Page<SSCourseAndTMsgInterfaceVo> findAllSelf(Pageable pageable);

    @Query("select c.code as code,c.name as name,c.credit as credit,t.name as teacherName " +
            "from Course c,Teacher t where c.name like concat('%',:name,'%') and c.teacherId=t.id")
    Page<SSCourseAndTMsgInterfaceVo> findByCourseName(String name,Pageable pageable);

    Course findByCodeAndTeacherId(String code, Integer id);

    Course findByName(String name);

    Course findByCode(String Code);

    Course findByCodeAndIdIsNot(String code, Integer id);

    Course findByNameAndIdIsNot(String name, Integer id);

    Page<Course> findByNameIsContaining(String courseName, Pageable page);

    Page<Course> findByTeacherId(Integer id, Pageable page);

    Page<Course> findByNameIsContainingAndTeacherId(String name, Integer id, Pageable page);

    @Query("from Course where id=:id")
    Course findByIdSelf(Integer id);

    @Transactional
    @Query(value = "SELECT * FROM select_course sc WHERE sc.course_code IN (SELECT c.code FROM course c WHERE c.id=?1)", nativeQuery = true)
    SelectCourse checkSelectCourse(Integer id);

    Course getCourseByCode(String code);

    List<Course> findAllByTeacherId(Integer id);

}
